<template>
  <div class="app-container">
    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="98px">
      <el-form-item label="关联订单编号" prop="orderNo">
        <el-input v-model="queryParams.orderNo" placeholder="请输入关联订单编号" clearable
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.CHECK_STATUS)"
                     :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
<!--      <el-form-item label="检测结果" prop="checkStatus">-->
<!--        <el-select v-model="queryParams.checkStatus" placeholder="请选择检测结果" clearable size="small">-->
<!--          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.CHECK_RESULT)"-->
<!--                     :key="dict.value" :label="dict.label" :value="dict.value"/>-->
<!--        </el-select>-->
<!--      </el-form-item>-->
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker v-model="queryParams.createTime" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss"
                        type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"
                        :default-time="['00:00:00', '23:59:59']"/>
      </el-form-item>

      <el-form-item label="拆包时间" prop="openTime">
        <el-date-picker v-model="queryParams.openTime" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss"
                        type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"
                        :default-time="['00:00:00', '23:59:59']"/>
      </el-form-item>

      <el-form-item label="报告时间" prop="reportTime">
        <el-date-picker v-model="queryParams.reportTime" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss"
                        type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"
                        :default-time="['00:00:00', '23:59:59']"/>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="openForm(undefined)"
                   v-hasPermi="['system:order-check-info:create']">新增
        </el-button>
      </el-col>-->
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                   :loading="exportLoading"
                   v-hasPermi="['system:order-check-info:export']">导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="id" align="center" prop="id"/>
      <el-table-column label="关联订单编号" align="center" prop="orderNo"/>
      <el-table-column label="订单克重" align="center" prop="orderWeight"/>
      <el-table-column label="复称克重" align="center" prop="checkWeight"/>
      <el-table-column label="熔后纯度(%)" align="center" prop="checkPurity"/>
      <el-table-column label="熔后克重" align="center" prop="meltWeight"/>
      <el-table-column label="状态" align="center" prop="status">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.CHECK_STATUS" :value="scope.row.status"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="检测结果" align="center" prop="checkStatus">-->
<!--        <template v-slot="scope">-->
<!--          <dict-tag :type="DICT_TYPE.CHECK_RESULT" :value="scope.row.checkStatus"/>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="拆包视频" align="center" prop="openVideoUrl">
        <template v-slot="scope">
          <el-button v-if="scope.row.openVideoUrl" size="mini" type="text" icon="el-icon-view" @click="openNewWindow(scope.row.openVideoUrl)">
            查看视频
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="拆包视频上传时间" align="center" prop="openTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.openTime) }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="检测视频地址url" align="center" prop="checkVideoUrl"/>
      <el-table-column label="检测视频上传时间" align="center" prop="checkVideoTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.checkVideoTime) }}</span>
        </template>
      </el-table-column>-->
      <el-table-column label="检测报告图片" align="center" prop="reportImg">
        <template v-slot="scope">
        <div v-if="scope.row.reportImg">
        <el-image style="width: 50px; height: 50px" :src="scope.row.reportImg" :preview-src-list="[scope.row.reportImg]">
          </el-image>
        </div>
        <div v-else>
        </div>

        </template>
      </el-table-column>
      <el-table-column label="检测报告上传时间" align="center" prop="reportTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.reportTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="描述" align="center" prop="remark"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template v-slot="scope">
          <!-- 状态:0待检测1检测中2已完成3已取消 -->
          <el-button v-if="scope.row.status == 0 || scope.row.status == 1" size="mini" type="text" icon="el-icon-edit" @click="handleOpenDialog(scope.row)"
                     v-hasPermi="['system:order-check-info:openVideoUpload']">拆包视频
          </el-button>
          <el-button  v-if="scope.row.status == 1" size="mini" type="text" icon="el-icon-edit" @click="handleReportDialog(scope.row)"
                     v-hasPermi="['system:order-check-info:reportUpload']">上传报告
          </el-button>

<!--          <el-button size="mini" type="text" icon="el-icon-edit" @click="openForm(scope.row.id)"
                     v-hasPermi="['system:order-check-info:update']">修改
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['system:order-check-info:delete']">删除
          </el-button>-->
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>
    <!-- 对话框(添加 / 修改) -->
    <OrderCheckInfoForm ref="formRef" @success="getList"/>


    <!-- 上传报告 弹框 openFormRef -->
    <el-dialog title="上传报告" :visible.sync="reportDialogVisible" width="45%" v-dialogDrag append-to-body>
      <el-form ref="reportFormRef" :model="reportFormData" :rules="reportFormRules" v-loading="formLoading" label-width="130px">
        <el-form-item label="熔后纯度(%)" prop="checkPurity">
          <el-input-number v-model="reportFormData.checkPurity" :precision="2" :step="0.01" :max="100" :min="0"></el-input-number>
        </el-form-item>
        <el-form-item label="熔后克重(g)" prop="meltWeight">
          <el-input-number v-model="reportFormData.meltWeight" :precision="3" :step="0.001" :min="0"></el-input-number>
        </el-form-item>
        <el-form-item label="报告图片" prop="reportImg">
          <ImageUpload v-model="reportFormData.reportImg" :limit="1"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleReportSubmit" :disabled="formLoading">确 定</el-button>
        <el-button @click="reportDialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 拆包视频 弹框 openFormRef -->
    <el-dialog title="拆包视频上传" :visible.sync="openDialogVisible" width="45%" v-dialogDrag append-to-body>
      <el-form ref="openFormRef" :model="openFormData" :rules="openFormRules" v-loading="formLoading" label-width="130px">
        <el-form-item label="复称克重" prop="checkWeight">
<!--          <el-input v-model="openFormData.checkWeight" placeholder="请输入复称克重" maxlength="10"/>-->
          <el-input-number v-model="openFormData.checkWeight" :precision="3" :step="0.001" :max="100000" :min="0.001"></el-input-number>
        </el-form-item>
        <el-form-item label="拆包视频" prop="openVideoUrl">
          <FileUpload v-model="openFormData.openVideoUrl" :limit="1" :fileType="['mp4', 'avi']" :fileSize="30"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleOpenVideoUploadSubmit" :disabled="formLoading">确 定</el-button>
        <el-button @click="openDialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import * as OrderCheckInfoApi from '@/api/system/ordercheckinfo';
import OrderCheckInfoForm from './OrderCheckInfoForm.vue';
import ImageUpload from '@/components/ImageUpload';
import FileUpload from '@/components/FileUpload';

export default {
  name: "OrderCheckInfo",
  components: {
    OrderCheckInfoForm,
    ImageUpload,
    FileUpload
  },
  data() {
    return {
      formLoading: false,
      reportDialogVisible: false,
      reportFormData: {
        id: null,
        reportImg: null,
        checkPurity: null,
        meltWeight: null
      },
      reportFormRules: {
        reportImg: [{required: true, message: '不能为空', trigger: 'blur'}],
        checkPurity: [{required: true, message: '不能为空', trigger: 'blur'}],
        meltWeight: [{required: true, message: '不能为空', trigger: 'blur'}],
      },

      openDialogVisible: false,
      openFormData: {
        id: null,
        openVideoUrl: null,
        checkWeight: null
      },
      openFormRules: {
        openVideoUrl: [{required: true, message: '不能为空', trigger: 'blur'}],
        checkWeight: [{required: true, message: '不能为空', trigger: 'blur'}],
      },

      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单检测信息列表
      list: [],
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      // 选中行
      currentRow: {},
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        orderNo: null,
        status: null,
        checkStatus: null,
        createTime: [],
        openTime: [],
        reportTime: [],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    openNewWindow(url) {
      window.open(url, '_blank')
    },
    /** 查询列表 */
    async getList() {
      try {
        this.loading = true;
        const res = await OrderCheckInfoApi.getOrderCheckInfoPage(this.queryParams);
        this.list = res.data.list;
        this.total = res.data.total;
      } finally {
        this.loading = false;
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 添加/修改操作 */
    openForm(id) {
      this.$refs["formRef"].open(id);
    },
    /** 删除按钮操作 */
    async handleDelete(row) {
      const id = row.id;
      await this.$modal.confirm('是否确认删除订单检测信息编号为"' + id + '"的数据项?')
      try {
        await OrderCheckInfoApi.deleteOrderCheckInfo(id);
        await this.getList();
        this.$modal.msgSuccess("删除成功");
      } catch {
      }
    },
    /** 导出按钮操作 */
    async handleExport() {
      await this.$modal.confirm('是否确认导出所有订单检测信息数据项?');
      try {
        this.exportLoading = true;
        const data = await OrderCheckInfoApi.exportOrderCheckInfoExcel(this.queryParams);
        this.$modal.msgSuccess("提交下载申请成功，稍后在下载记录中查看");
        // this.$download.excel(data, '订单检测信息.xls');
      } catch {
      } finally {
        this.exportLoading = false;
      }
    },
    /** 上传报告 弹框 */
    async handleReportDialog(row) {
      this.reportDialogVisible = true
      this.reportFormData = row
    },
    /** 上传报告 提交 */
    async handleReportSubmit() {
        await this.$refs["reportFormRef"].validate();
        this.formLoading = true;
        await this.$modal
        .confirm(
          '是否确认提交订单编号为"' + this.reportFormData.orderNo + '"的数据项?'
        )
        .then(() => {
          OrderCheckInfoApi.reportUpload(this.reportFormData).then((res) => {
            this.getList();
            this.$modal.msgSuccess("提交报告成功");
            this.reportDialogVisible = false;
            this.formLoading = false;
          });
        })
        .catch(() => {
          this.getList();
          this.formLoading = false;
          return false;
        });
    },


    /** 拆包视频上传 弹框 */
    async handleOpenDialog(row) {
      this.openDialogVisible = true
      this.openFormData = row
    },

    /** 拆包视频上传 提交 */
    async handleOpenVideoUploadSubmit() {
      await this.$refs["openFormRef"].validate();
      this.formLoading = true;
      await this.$modal.confirm('是否确认提交订单编号为"' + this.openFormData.orderNo + '"的数据项?')
      try {
        await OrderCheckInfoApi.openVideoUpload(this.openFormData);
        await this.getList();
        this.$modal.msgSuccess("提交报告成功");
        this.openDialogVisible = false
        this.formLoading = false;
      } catch {
        this.formLoading = false;
        await this.getList();
      }
    },

  }
};
</script>
